package pe.gob.foncodes.ssa.web.seguridad.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pe.gob.foncodes.ssa.bs.common.util.Constante;
import pe.gob.foncodes.ssa.bs.common.util.StringHelper;
import org.apache.log4j.Logger;
import pe.gob.foncodes.ssa.web.controller.LoginController;

/**
 * Servlet Filter implementation class Seguridad
 */
public class Seguridad implements Filter {
    protected static Logger log = Logger.getLogger(Seguridad.class);
    private static String URL_ACCESO_DENEGADO = "/index.jsp";
    private static String[] URL_ACCESO = new String[]{"/index.jsp", "/pages/login.jsf" , "/pages/home.jsf" , "/SistemaSeguridadWS"};
    private StringHelper stringHelper = new StringHelper();

    /**
     * Default constructor. 
     */
    public Seguridad() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        // place your code here
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpSession session = ((HttpServletRequest) request).getSession();
        //if(sonImagenes(httpRequest.getServletPath())){
            //return;
        //}
        //log.info("httpRequest.getServletPath()="+httpRequest.getServletPath());
        if (stringHelper.findStringInArray(httpRequest.getServletPath(), URL_ACCESO)) {
            chain.doFilter(request, response);
            return;
        }
        LoginController login = (LoginController)session.getAttribute("loginController");
        //log.info("login.isLogueado() = "+login.isLogueado());
        if (session == null || session.getAttribute(Constante.SESSION_USER) == null) {
            httpResponse.sendRedirect(httpRequest.getContextPath() + URL_ACCESO_DENEGADO);
        } else {
            chain.doFilter(request, response);
        }
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    @Override
    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }
    
    private boolean sonImagenes(String uri) {
        if (uri.endsWith(".jsf")  || uri.endsWith(".js") || uri.endsWith(".css") || uri.endsWith(".png") || uri.endsWith(".gif") || uri.endsWith(".jpg")) {
            return true;
        } else {
            return false;
        }                
    }
}
